package com.demo.ncfsjl;

import java.util.Date;

import com.demo.common.model.NCFSJL;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * 
 * NC_FSJLService
 * 所有 sql 与业务逻辑写在 Service 中，不要放在 Model 中，更不
 * 要放在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
public class NCFSJLService {
	
	@Before(NCFSJLValidator.class)
	public void save(){
		
	}
	
	/**
	 * 所有的 dao 对象也放在 Service 中，并且声明为 private，避免 sql 满天飞
	 * sql 只放在业务层，或者放在外部 sql 模板，用模板引擎管理：
	 * 			http://www.jfinal.com/doc/5-13
	 */
	private NCFSJL dao = new NCFSJL().dao();
	
	public Page<NCFSJL> paginate(int pageNumber, int pageSize) {
		return dao.paginate(pageNumber, pageSize, "select *", "from NC_FSJL order by RQ desc");
	}
	
	public Page<NCFSJL> paginate(int pageNumber, int pageSize,String startDate,String endDate) {

		StringBuilder sql = new StringBuilder();
		sql.append("from NC_FSJL WHERE 1=1");
		if(startDate != null && startDate.length()>1) {
			sql.append(" AND RQ>='"+startDate+"'");
		}
		if(endDate != null && endDate.length()>1) {
			sql.append(" AND RQ<='"+endDate+"'");
		}
		sql.append(" ORDER BY RQ DESC");
		return dao.paginate(pageNumber, pageSize, "select *", sql.toString());
	}
	
	public NCFSJL findById(String id) {		
		return dao.findById(id);
	}
	
	public void deleteById(String id) {
		dao.deleteById(id);
	}
	
	
	public void insertNCFSJL(String djbh ,String sendXml ,String result ,String retXml, String comment){
		String ncbillid = "";
		if("1".equals(result)){
			ncbillid = comment;
			comment = "";
		}
		Record rec = new Record().set("RQ", new Date()).set("DJBH", djbh).set("SENDXML", sendXml).set("RESULT", result).set("NCBILLID", ncbillid).set("RETXML", retXml).set("COMMENT", comment);
		Db.save("NC_FSJL", rec);
	}

}
